<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use app\admin\model\StaffCert;

/*
 * 美容师
 */

class Staff extends Model {
    
    /*
     * 修改美容师的级别
     */
    public static function saveStaffRank($staffId,$newRank){
        //由代队长升级为队长 ai_staff_cert里的 cert_num（修改第七位）和staff_rank  ai_staff表里的staff_rank
        $cert_num = StaffCert::where('staff_id',$staffId)->value('cert_num');
        $newRankCode = StaffRank::where('id',$newRank)->value('code');
        $cert_num = substr_replace($cert_num,$newRankCode,6,1);
        
        Db::startTrans();
        //记录晋升
        $old_rank = Staff::alias('s')
                ->join('staff_rank r','r.id = s.staff_rank','left')
                ->where('s.id',$staffId)
                ->field('r.name,r.id')
                ->find();
        $new_rankname = StaffRank::where('id',$newRank)->value('name');
        $exaltData['staff_id'] = $staffId;
        $exaltData['type'] = 1;
        $exaltData['old_rank'] = $old_rank['id'];
        $exaltData['old_rankname'] = $old_rank['name'];
        $exaltData['new_rank'] = $newRank;
        $exaltData['new_rankname'] = $new_rankname;
        $exaltData['add_time'] = date('Y-m-d H:i:s');
        $exaltResult = StaffExaltLogs::insert($exaltData);
        //修改证书编号和级别
        $data['staff_rank'] = $newRank;
        $data['cert_num'] = $cert_num;
        $certResult = StaffCert::where('staff_id',$staffId)->update($data);
        //修改美容师级别 和证书状态
        $staffdata['staff_rank'] = $newRank;
        $staffdata['cert_status'] = 2;
        $staffResult = Staff::where('id',$staffId)->update($staffdata);
        
        if($certResult && $staffResult && $exaltResult){
            Db::commit();
            return true;
        }  else {
            Db::rollback();
            return false;
        }
        
        
    }

}
